package com.northwest_man.refactor_tool;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// 此工具类需要搭配Constants一起使用
public class VerifyIfPasteFileSameAsCopyFile {

    private static final boolean copy = true;

    public static void main() {
        FileUtil.recursionFolder(new File(Constants.pastedParentFolder), null, new Handler<Void>() {
            @Override
            public Void doHandle(File curFile, List<String> allFileAbsolutePath) {
                final String toFileAPath = curFile.getAbsolutePath();
                final String regex = Constants.pastedParentFolder + "(/[^ ]+?([^/]+)\\.java)";
                final Pattern pattern = Pattern.compile(regex);
                final Matcher matcher = pattern.matcher(toFileAPath);
                if (matcher.find()) {
                    final String fromFileAPath = Constants.copyParentFolder + matcher.group(1);
                    final String fromFileNameWithoutSuffix = matcher.group(2);


                    try {
                        if (!FileUtils.contentEquals(new File(fromFileAPath), new File(toFileAPath))) {
                            System.out.println("内容不同：" + toFileAPath);
                            if (copy) {
                                Copy.cp(fromFileNameWithoutSuffix);
                            }
                        } else {
//                            System.out.println("内容相同："+toFileAPath);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("比较时发生异常：" + toFileAPath);
                    }
                } else {
                    System.out.println("正则无法匹配路径：" + toFileAPath);
                }
                return null;
            }
        });
    }
}
